package apk.yoursms;

import android.content.Context;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
//import apk.yoursms.views.TextView;
import android.widget.Toast;
import apk.yoursms.views.TextView;

class SimplerExpandableListAdapter extends BaseExpandableListAdapter {
    private Context mContext;
    private String[][] mContents;
    private String[] mTitles;

    public SimplerExpandableListAdapter(Context context, String[] titles,
	    String[][] contents) {
	super();
	if (titles.length != contents.length) {
	    throw new IllegalArgumentException(
		    "Titles and Contents must be the same size.");
	}

	mContext = context;
	mContents = contents;
	mTitles = titles;
    }

    public String getChild(int groupPosition, int childPosition) {
	return mContents[groupPosition][childPosition];
    }

    public long getChildId(int groupPosition, int childPosition) {
	return 0;
    }

    public View getChildView(int groupPosition, int childPosition,
	    boolean isLastChild, View convertView, ViewGroup parent) {
	if (convertView == null) {
	    LayoutInflater infalInflater = (LayoutInflater) mContext
		    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	    convertView = infalInflater.inflate(R.layout.child_layout, null);
	}
	TextView tv = (TextView) convertView.findViewById(R.id.tvChild);
	tv.setText(mContents[groupPosition][childPosition]);

	return convertView;
    }

    public int getChildrenCount(int groupPosition) {	
	if (mContents == null) {
	    Toast toast = Toast.makeText(mContext,
		    mContext.getString(R.string.should_update_base), Toast.LENGTH_LONG);
	    toast.setGravity(Gravity.CENTER, 0, 0);
	    toast.show();
	    Log.d("getChildrenCount", "mContents is Null");
	    return 0;
	} else {
	    return mContents[groupPosition].length;	    
	}
    }

    public String[] getGroup(int groupPosition) {
	return mContents[groupPosition];
    }

    public int getGroupCount() {
	return mContents.length;
    }

    public long getGroupId(int groupPosition) {
	return 0;
    }

    public View getGroupView(int groupPosition, boolean isExpanded,
	    View convertView, ViewGroup parent) {
	if (convertView == null) {
	    LayoutInflater infalInflater = (LayoutInflater) mContext
		    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	    convertView = infalInflater.inflate(R.layout.group_layout, null);
	}
	TextView tv = (TextView) convertView.findViewById(R.id.tvGroup);
	tv.setText(mTitles[groupPosition]);

	if (mTitles[groupPosition].equalsIgnoreCase(mContext
		.getString(R.string.sms_love)))
	    tv.setCompoundDrawablesWithIntrinsicBounds(R.drawable.sms_love, 0,
		    0, 0);
	else if (mTitles[groupPosition].equalsIgnoreCase(mContext
		.getString(R.string.sms_skandal)))
	    tv.setCompoundDrawablesWithIntrinsicBounds(R.drawable.sms_skandal,
		    0, 0, 0);
	else if (mTitles[groupPosition].equalsIgnoreCase(mContext
		.getString(R.string.sms_congratulations)))
	    tv.setCompoundDrawablesWithIntrinsicBounds(
		    R.drawable.sms_congratulations, 0, 0, 0);
	else if (mTitles[groupPosition].equalsIgnoreCase(mContext
		.getString(R.string.sms_humor)))
	    tv.setCompoundDrawablesWithIntrinsicBounds(R.drawable.sms_humor, 0,
		    0, 0);

	return convertView;
    }

    public boolean hasStableIds() {
	return false;
    }

    public boolean isChildSelectable(int groupPosition, int childPosition) {
	return true;
    }

}
